package com.tianxin.workbao.workBao.adapter;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.tianxin.workbao.workBao.Activity.BaseActivity;
import com.tianxin.workbao.workBao.R;
import com.tianxin.workbao.workBao.bean.Caraddoilcard;
import com.tianxin.workbao.workBao.dataProvide.CaraddoilcardProvide;
import com.tianxin.workbao.workBao.holder.EmptyHolder;
import com.tianxin.workbao.workBao.listener.UpdateListViewListener;
import com.tianxin.workbao.workBao.utils.common.TimeUtils;
import com.tianxin.workbao.workBao.utils.common.ToastUtils;
import com.tianxin.workbao.workBao.view.ExpandView;

import java.util.List;

/**
 * Created by hacker on 2017/7/26.
 */

public class CaraddoilcardAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int TYPE_EMPTY = 0;
    private static final int TYPE_DEFAULT = 1;
    private List<Caraddoilcard> caraddoilcardList;
    private Context mContext;
    private int mPosition;


    public CaraddoilcardAdapter(List<Caraddoilcard> list, Context context) {
        this.caraddoilcardList = list;
        this.mContext = context;
    }

    @Override
    public int getItemViewType(int position) {
        if (caraddoilcardList.size() > 0) {
            return TYPE_DEFAULT;
        } else {
            return TYPE_EMPTY;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if (viewType == TYPE_DEFAULT) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.transfer_money_item, parent, false);
            return new ViewHolder(view);
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty, parent, false);
            return new EmptyHolder(view);
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            mPosition = position;
            ViewHolder viewHolder = (ViewHolder) holder;
            viewHolder.BindViewData(caraddoilcardList.get(mPosition));
            viewHolder.setViewListener(caraddoilcardList.get(mPosition));
        }
    }

    @Override
    public int getItemCount() {
        if (caraddoilcardList.size() > 0) {
            return caraddoilcardList.size();
        } else {
            return 1;
        }
    }

    public int getItemColumnSpan(int position) {
        switch (getItemViewType(position)) {
            case TYPE_DEFAULT:
                return 1;
            default:
                return 1;
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView arrowIV;
        ExpandView expandView;
        TextView numberTV;
        TextView statusLabelTV;
        TextView moneyTV;
        TextView timeTV;
        TextView remarkTV;
        TextView addUserTV;
        TextView oauthUserTV;
        TextView bankNameTV;
        TextView bankNumberTV;
        TextView carNumberTV;
        Button checkBtn;


        public ViewHolder(View itemView) {
            super(itemView);
            numberTV = itemView.findViewById(R.id.number);
            arrowIV = itemView.findViewById(R.id.arrow);
            expandView = itemView.findViewById(R.id.expandView);
            statusLabelTV = itemView.findViewById(R.id.status_label);
            timeTV = itemView.findViewById(R.id.time);
            moneyTV = itemView.findViewById(R.id.money);
            remarkTV = itemView.findViewById(R.id.remark);
            bankNameTV = itemView.findViewById(R.id.bank_name);
            bankNumberTV = itemView.findViewById(R.id.bank_number);
            addUserTV = itemView.findViewById(R.id.add_user);
            oauthUserTV = itemView.findViewById(R.id.oauth_user);
            carNumberTV = itemView.findViewById(R.id.car_number);
            checkBtn = itemView.findViewById(R.id.check_btn);
        }

        public void BindViewData(Caraddoilcard caraddoilcard) {
            numberTV.setText(caraddoilcard.getNumber());
            carNumberTV.setText(caraddoilcard.getPlatenumbers());
            if (caraddoilcard.getStatus() == 1) {
                checkBtn.setVisibility(View.GONE);
                statusLabelTV.setText("审");
                timeTV.setText(TimeUtils.parseDate(caraddoilcard.getTrueaddtime()));
                oauthUserTV.setText("审核：" + caraddoilcard.getOptionusername() + "\n"
                        + TimeUtils.parseDateTime(caraddoilcard.getTrueaddtime()));
            } else {
                checkBtn.setVisibility(View.VISIBLE);
                statusLabelTV.setText("未审");
                timeTV.setText(TimeUtils.parseDate(caraddoilcard.getAddtime()));
                oauthUserTV.setText("未审核");
                oauthUserTV.setTextColor(mContext.getResources().getColor(R.color.red_btn_bg_color));
            }
            addUserTV.setText("添加：" + caraddoilcard.getAddusername() + "\n"
                    + TimeUtils.parseDateTime(caraddoilcard.getAddtime()));

            bankNameTV.setText(caraddoilcard.getOilcardname() + "  " + (caraddoilcard.getOiltype() == 1 ? "主公司卡" : "分公司卡"));
            bankNumberTV.setText(caraddoilcard.getCardHolder() + "  " + caraddoilcard.getOilcardnumber());
            moneyTV.setText("金额" + caraddoilcard.getMoney());
            int length = caraddoilcard.getRemark().length();
            if (length > 1 && caraddoilcard.getRemark() != "0") {
                remarkTV.setText("备注：" + caraddoilcard.getRemark());
                remarkTV.setVisibility(View.VISIBLE);
            } else {
                remarkTV.setVisibility(View.GONE);
            }
        }

        public void setViewListener(final Caraddoilcard caraddoilcard) {
            arrowIV.setOnClickListener(view -> {
                switch (view.getId()) {
                    case R.id.arrow:
                        if (expandView.isExpand()) {
                            expandView.collapse();
                            arrowIV.setImageResource(R.drawable.ic_arrow_drop_down_black_24dp);
                        } else {
                            expandView.expand();
                            arrowIV.setImageResource(R.drawable.ic_arrow_drop_up_black_24dp);
                        }
                        break;
                }
            });
            checkBtn.setOnClickListener(view -> {
                if (view.getId() == R.id.check_btn) {
                    getCaraddoilcardProvide().check(caraddoilcard);
                }
            });
        }


        public CaraddoilcardProvide getCaraddoilcardProvide() {
            CaraddoilcardProvide provide = new CaraddoilcardProvide(mContext, new UpdateListViewListener() {
                @Override
                public void onRefreshData(Object result) {

                }

                @Override
                public void onLoadMoreData(Object result) {

                }

                @Override
                public void showMessage(String msg) {
                    if (msg == "ok") {
                        expandView.collapse();
                        caraddoilcardList.remove(mPosition);
                        notifyDataSetChanged();
                        ToastUtils.showLong("操作成功");
                    } else if (msg.contains("failure")) {
                        ToastUtils.showLong("操作失败！\n " + msg.substring(msg.indexOf("failure") + 1));
                    } else {
                        ToastUtils.showLong(msg);
                    }
                }

                @Override
                public void showProgress() {
                    BaseActivity activity = (BaseActivity) CaraddoilcardAdapter.this.mContext;
                    activity.getSweetAlertDialog(activity).showDialog("", 1200);
                }

                @Override
                public void hideProgress() {
                    BaseActivity activity = (BaseActivity) CaraddoilcardAdapter.this.mContext;
                    activity.getSweetAlertDialog(activity).dismiss();
                }
            }, null);
            return provide;
        }
    }
}
